package org.ihelpuoo.orm.handler.rs;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.ihelpuoo.db.ResultSetProxy;
import org.ihelpuoo.orm.filter.Filter;
import org.ihelpuoo.orm.handler.ObjectProcessor;
import org.ihelpuoo.orm.filter.FieldFilter;
import org.ihelpuoo.orm.handler.FieldHandler;
import org.ihelpuoo.orm.handler.FieldHandlers;
import org.ihelpuoo.orm.handler.rs.RsReadArrayFieldHandler;
import org.ihelpuoo.orm.handler.rs.RsReadObjectFieldHandler;
import org.ihelpuoo.orm.handler.rs.RsReadListFieldHandler;
import org.ihelpuoo.orm.handler.rs.RsReadMapFieldHandler;
import org.ihelpuoo.orm.handler.rs.RsReadNormalFieldHandler;
import org.ihelpuoo.orm.FieldRule;
import org.ihelpuoo.orm.IIIException;
import org.ihelpuoo.orm.IIIManual;
import org.ihelpuoo.orm.ObjectRule;
import org.ihelpuoo.util.Pager;

public class ResultSetProcessor extends ObjectProcessor
{

	private FieldHandler	normalFieldHandler;

	public ResultSetProcessor(IIIManual manual)
	{
		super(manual);
		ObjectProcessor bp = manual.getConnectionProcessor();
		normalFieldHandler = new RsReadNormalFieldHandler(bp);
		this.getFieldHandlers = new FieldHandlers(normalFieldHandler,
				new RsReadListFieldHandler(bp),new RsReadArrayFieldHandler(bp),
				new RsReadMapFieldHandler(bp),new RsReadObjectFieldHandler(bp));
	}

	@Deprecated
	@Override
	public boolean delete(ObjectRule r, Object src, Object obj, FieldFilter ignore)
			throws IIIException
	{
		throw new IIIException(this.getClass().getName() + " do not support deleteBean()!");
	}

	@Deprecated
	@Override
	public int delete(ObjectRule r, Object src, List list, int off, int len) throws IIIException
	{
		throw new IIIException(this.getClass().getName() + " do not support clearBeans()!");
	}

	@Deprecated
	@Override
	public Object insert(ObjectRule r, Object src, Object obj, FieldFilter actived,
			FieldFilter locked) throws IIIException
	{
		throw new IIIException(this.getClass().getName() + " do not support insertBean()!");
	}

	@Deprecated
	@Override
	public List query(ObjectRule r, Object src, Class beanType, Filter filter, FieldFilter actived,
			FieldFilter locked, Pager pager, Object refer) throws IIIException
	{
		throw new IIIException(this.getClass().getName() + " do not support queryBeanList()!");
	}

	@Deprecated
	@Override
	public Object update(ObjectRule r, Object src, Object obj, FieldFilter actived,
			FieldFilter locked) throws IIIException
	{
		throw new IIIException(this.getClass().getName() + " do not support updateBean()!");
	}

	@Override
	public Object fetch(ObjectRule r, Object src, Object obj, FieldFilter actived,
			FieldFilter locked, String prefix) throws IIIException
	{
		r.checkDatabaseConfig(((ResultSetProxy)src).getConnection(),obj);
		Iterator<FieldRule> it = r.rules().iterator();
		List<FieldRule> normalFields = new LinkedList<FieldRule>();
		List<FieldRule> comFields = new LinkedList<FieldRule>();

		while (it.hasNext())
		{
			FieldRule fr = it.next();
			if (fr.isIgnoreInResultSet(actived,locked))
				continue;
			if (fr.isComponentType())
				comFields.add(fr);
			else
				normalFields.add(fr);
		}
		it = normalFields.iterator();
		while (it.hasNext())
			normalFieldHandler.process(r,it.next().getField(),src,obj,null,null,null);
		it = comFields.iterator();
		while (it.hasNext())
			getFieldHandlers.process(r,it.next().getField(),src,obj,prefix);
		return obj;
	}
}
